package com.cardbaobao.cardbabyclient.ui.fragment.newindexitem;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.adapter.CardGiftRcAdapter;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.model.IndexLayout;
import com.cardbaobao.cardbabyclient.widget.RecyclerViewWithFooter.OnTopListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Description:
 * Created by FHW
 * on 2017/6/12
 */

public class CardGiftFragment extends BaseFragment {
    @BindView(R.id.rv_card_gift)
    RecyclerView rv_card_gift;

    private CardGiftRcAdapter mAdapter;
    private List<IndexLayout.HomeTabBean> list;

    private OnTopListener onTopListener;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_card_gfit;
    }

    @Override
    protected void initViewFinish(View view, Bundle savedInstanceState) {

        list = new ArrayList<>();
        mAdapter = new CardGiftRcAdapter(getActivity(), list);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 6);
        rv_card_gift.setLayoutManager(layoutManager);
        rv_card_gift.setAdapter(mAdapter);

        rv_card_gift.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                int visiblePosition = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (visiblePosition == 0) {
                        if (onTopListener != null)
                            if(mAdapter.getItemCount()>4) {
                                onTopListener.onTop();
                            }
                    }
                }
            }
        });

    }

    public void onTopListener(OnTopListener onTopListener) {
        this.onTopListener = onTopListener;
    }

    public void setData(List<IndexLayout.HomeTabBean> cardGiftList){
        list.clear();
        list.addAll(cardGiftList);
        mAdapter.notifyDataSetChanged();
    }

}
